package com.example.jasonsun.test;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.jasonsun.sharelib.PopupDialogShare;
import com.example.jasonsun.sharelib.ShareInstance;
import com.example.jasonsun.sharelib.config.ConfigSetter;

public class MainActivity extends AppCompatActivity {

    private TextView mText;

    private PopupDialogShare.onShareModelSelectedListener mListener;
    private String mTitle = "test title";
    private String mContent = "hello content , this is for test";
    private String mImgUrl = "http://m.img10.starclouds.com.cn/20160601/a8095135-f386-45fd-8d28-a5c7bc149340_1.jpg@100h_100w";
    //        private String mImgUrl = "http://m.img10.starclouds.com.cn/20160601/a8095135-f386-45fd-8d28-a5c7bc149340_1.jpg";
    private String mWebUrl = "http://192.168.199.234:10010/nshare/show.do?projectId=215&shareDes=%E7%83%A6%E7%83%A6%E7%83%A6%E5%8F%91";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initShare();

        mText = (TextView) findViewById(R.id.text);
    }

    private void initShare() {
        ConfigSetter config = new ConfigSetter();
        config.setSinaAppKey("3630806295");
        config.setSinaAppSecret("1b2f34637fbdf95042486406affd7ef3");
        config.setQQAppId("222222");
        ShareInstance.getInstance().init(config);
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_xl:
                loginSina();
                break;
            case R.id.login_qq:
                loginQQ();
                break;
            case R.id.login_weichat:
                break;
            case R.id.share:
                share(v);
                break;
        }
    }

    private void loginSina() {
        ShareInstance.getInstance().login(this, ShareInstance.SharePlatFormId.XINLANG_WEIBO,
                new ShareInstance.IGetResult() {
                    @Override
                    public void onSuccess(Bundle result) {
                        String id = result.getString(ShareInstance.KEY_OPEN_ID);
                        Log.d("hello", "application_openID: " + id);
                        mText.setText("appid: " + id);
                    }

                    @Override
                    public void onFailure(int errCode, String errMessage) {
                        Log.d("hello", "application: " + errMessage);
                        String msg;
                        if (errMessage != null) {
                            msg = errMessage.equals("cancel") ? "已取消" : errMessage;
                        } else {
                            msg = "";
                        }
                        Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
                    }
                });
    }

    private void loginQQ() {
        ShareInstance.getInstance().login(this, ShareInstance.SharePlatFormId.QQ_FRINEDS,
                new ShareInstance.IGetResult() {
                    @Override
                    public void onSuccess(Bundle result) {
                        String id = result.getString(ShareInstance.KEY_OPEN_ID);
                        Log.d("hello", "application_openID: " + id);
                        mText.setText("appid: " + id);
                    }

                    @Override
                    public void onFailure(int errCode, String errMessage) {
                        Log.d("hello", "application: " + errMessage);
                        String msg;
                        if (errMessage != null) {
                            msg = errMessage.equals("cancel") ? "已取消" : errMessage;
                        } else {
                            msg = "";
                        }
                        Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
                    }
                });
    }

    private void share(View v) {
        PopupDialogShare dlg = new PopupDialogShare(this, new PopupDialogShare.onShareModelSelectedListener() {
            @Override
            public void onSelected(ShareInstance.SharePlatFormId shareTo) {
                ShareInstance.getInstance().shareWeb(MainActivity.this, shareTo,
                        mTitle, mContent, mImgUrl, mWebUrl, new ShareInstance.IGetResult() {
                            @Override
                            public void onSuccess(Bundle result) {
                                Log.e("hello", "share sucdess");
                                Toast.makeText(MainActivity.this, "分享成功", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onFailure(int errCode, String errMessage) {
                                Log.e("hello", "share fail " + errMessage);
                                String msg;
                                if (errMessage != null) {
                                    msg = errMessage.equals("cancel") ? "已取消" : errMessage;
                                } else {
                                    msg = "";
                                }
                                Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
                            }
                        });
            }
        });

        dlg.showPopup(v);
    }

}
